﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.Metro;
using DevComponents.DotNetBar;
using System.Diagnostics;
using DevComponents.AdvTree;
using DevComponents.DotNetBar.Metro.ColorTables;
using GUI.View;
namespace GUI
{
    public partial class MainForm : MetroAppForm
    {
        StartControl _StartControl = null; // Start control displayed on startup
        AppCommands _Commands = null; // All application commands
        public MainForm()
        {
            InitializeComponent();
            // Prepare commands
            //if (new DBConnection().open())
            //    MessageBox.Show("Ket noi thanh cong");
            //else
            //    MessageBox.Show("Ket noi that bai");
            _Commands = new AppCommands();

            _Commands.ToggleStartControl = new Command(components);
            _Commands.ToggleStartControl.Executed += new EventHandler(ToggleStartControlExecuted);
            //Danh Sách
            _Commands.ListKhamBenhCommands.New = new Command(components); // We pass in components from Form so the command gets disposed automatically when form is disposed
            _Commands.ListKhamBenhCommands.New.Executed += NewDanhSachKhamBenhControl;
            _Commands.ListKhamBenhCommands.Cancel = new Command(components);
            _Commands.ListKhamBenhCommands.Cancel.Executed += CancelDanhSachKhamBenh;


            //Khám Bệnh
            _Commands.ListBenhNhanCommands.New = new Command(components); // We pass in components from Form so the command gets disposed automatically when form is disposed
            _Commands.ListBenhNhanCommands.New.Executed += NewDanhSachBenhNhanControl;
            _Commands.ListBenhNhanCommands.Cancel = new Command(components);
            _Commands.ListBenhNhanCommands.Cancel.Executed += CancelDanhSachBenhNhan;

            //Tìm kiếm
            _Commands.TimKiemCommands.New = new Command(components); // We pass in components from Form so the command gets disposed automatically when form is disposed
            _Commands.TimKiemCommands.New.Executed += NewTimKiemControl;
            _Commands.TimKiemCommands.Cancel = new Command(components);
            _Commands.TimKiemCommands.Cancel.Executed += CancelFormSearch;

            //Lập hóa đơn
            _Commands.LapHoaDonCommands.New = new Command(components); // We pass in components from Form so the command gets disposed automatically when form is disposed
            _Commands.LapHoaDonCommands.New.Executed += NewLapHoaDonControl;
            _Commands.LapHoaDonCommands.Cancel = new Command(components);
            _Commands.LapHoaDonCommands.Cancel.Executed += CancelFormBill;


            //Thống Kê
            _Commands.ThongKeCommands.New = new Command(components); // We pass in components from Form so the command gets disposed automatically when form is disposed
            _Commands.ThongKeCommands.New.Executed += NewThongKeControl;
            _Commands.ThongKeCommands.Cancel = new Command(components);
            _Commands.ThongKeCommands.Cancel.Executed += CancelReport;

            //Tùy Chỉnh
            _Commands.TuyChinhCommands.New = new Command(components); // We pass in components from Form so the command gets disposed automatically when form is disposed
            _Commands.TuyChinhCommands.New.Executed += NewOptionControl;
            _Commands.TuyChinhCommands.Cancel = new Command(components);
            _Commands.TuyChinhCommands.Cancel.Executed += CancelOption;

            //Thông tin
            _Commands.ThongTinCommands.New = new Command(components); // We pass in components from Form so the command gets disposed automatically when form is disposed
            _Commands.ThongTinCommands.New.Executed += NewInfomationControl;
            _Commands.ThongTinCommands.Cancel = new Command(components);
            _Commands.ThongTinCommands.Cancel.Executed += CancelInfomation;


            this.SuspendLayout();
            _StartControl = new StartControl();
            _StartControl.Commands = _Commands;
            this.Controls.Add(_StartControl);
            _StartControl.BringToFront();
            _StartControl.SlideSide = DevComponents.DotNetBar.Controls.eSlideSide.Right;
            this.ResumeLayout(false);


        }
        DanhSachKhamBenhControl _listKhamBenh = null;
        void NewDanhSachKhamBenhControl(object sender, EventArgs e)
        {
            Debug.Assert(_listKhamBenh == null);
            _Commands.ListKhamBenhCommands.New.Enabled = false; // Disable new client command to prevent re-entrancy
            _listKhamBenh = new DanhSachKhamBenhControl();
            _listKhamBenh.Commands = _Commands;
            this.ShowModalPanel(_listKhamBenh, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;
        }

        private void CancelDanhSachKhamBenh(object sender, EventArgs e)
        {
            // Simply close client entry form "dialog"
            Debug.Assert(_listKhamBenh != null);
            CloseDanhSachKhamBenh();
        }
        private void CloseDanhSachKhamBenh()
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;

            _Commands.ListKhamBenhCommands.New.Enabled = true; // Enable new client command

            this.CloseModalPanel(_listKhamBenh, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            _listKhamBenh.Commands = null;
            _listKhamBenh.Dispose();
            _listKhamBenh = null;
        }


        //Khám Bệnh

        DanhSachBenhNhanControl _listBenhNhan = null;
        void NewDanhSachBenhNhanControl(object sender, EventArgs e)
        {
            Debug.Assert(_listBenhNhan == null);
            _Commands.ListBenhNhanCommands.New.Enabled = false; // Disable new client command to prevent re-entrancy
            _listBenhNhan = new DanhSachBenhNhanControl();
            _listBenhNhan.Commands = _Commands;
            this.ShowModalPanel(_listBenhNhan, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;
        }

        private void CancelDanhSachBenhNhan(object sender, EventArgs e)
        {
            // Simply close client entry form "dialog"
            Debug.Assert(_listBenhNhan != null);
            CloseDanhSachBN();
        }
        private void CloseDanhSachBN()
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;

            _Commands.ListBenhNhanCommands.New.Enabled = true; // Enable new client command

            this.CloseModalPanel(_listBenhNhan, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            _listBenhNhan.Commands = null;
            _listBenhNhan.Dispose();
            _listBenhNhan = null;
        }


        //Tìm kiếm
        TimKiemControl _search = null;

        void NewTimKiemControl(object sender, EventArgs e)
        {
            Debug.Assert(_search == null);
            _Commands.TimKiemCommands.New.Enabled = false; // Disable new client command to prevent re-entrancy
            _search = new TimKiemControl();
            _search.Commands = _Commands;
            this.ShowModalPanel(_search, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;
        }

        private void CancelFormSearch(object sender, EventArgs e)
        {
            // Simply close client entry form "dialog"
            Debug.Assert(_search != null);
            CloseFormSearch();
        }
        private void CloseFormSearch()
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;

            _Commands.TimKiemCommands.New.Enabled = true; // Enable new client command

            this.CloseModalPanel(_search, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            _search.Commands = null;
            _search.Dispose();
            _search = null;
        }

        //Lập Hóa đơn

        LapHoaDonControl _bill = null;
        void NewLapHoaDonControl(object sender, EventArgs e)
        {
            Debug.Assert(_search == null);
            _Commands.LapHoaDonCommands.New.Enabled = false; // Disable new client command to prevent re-entrancy
            _bill = new LapHoaDonControl();
            _bill.Commands = _Commands;
            this.ShowModalPanel(_bill, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;
        }

        private void CancelFormBill(object sender, EventArgs e)
        {
            // Simply close client entry form "dialog"
            Debug.Assert(_bill != null);
            CloseFormBill();
        }
        private void CloseFormBill()
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;

            _Commands.LapHoaDonCommands.New.Enabled = true; // Enable new client command

            this.CloseModalPanel(_bill, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            _bill.Commands = null;
            _bill.Dispose();
            _bill = null;
        }

        //Thống kê

        ThongKeControl _report = null;
        void NewThongKeControl(object sender, EventArgs e)
        {
            Debug.Assert(_report == null);
            _Commands.ThongKeCommands.New.Enabled = false; // Disable new client command to prevent re-entrancy
            _report = new ThongKeControl();
            _report.Commands = _Commands;
            this.ShowModalPanel(_report, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;
        }

        private void CancelReport(object sender, EventArgs e)
        {
            // Simply close client entry form "dialog"
            Debug.Assert(_report != null);
            CloseReport();
        }
        private void CloseReport()
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;

            _Commands.ThongKeCommands.New.Enabled = true; // Enable new client command

            this.CloseModalPanel(_report, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            _report.Commands = null;
            _report.Dispose();
            _report = null;
        }

        //Tùy chỉnh

        OptionControl _option = null;
        void NewOptionControl(object sender, EventArgs e)
        {
            Debug.Assert(_option == null);
            _Commands.TuyChinhCommands.New.Enabled = false; // Disable new client command to prevent re-entrancy
            _option = new OptionControl();
            _option.Commands = _Commands;
            this.ShowModalPanel(_option, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;
        }

        private void CancelOption(object sender, EventArgs e)
        {
            // Simply close client entry form "dialog"
            Debug.Assert(_option != null);
            CloseOption();
        }
        private void CloseOption()
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;

            _Commands.TuyChinhCommands.New.Enabled = true; // Enable new client command

            this.CloseModalPanel(_option, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            _option.Commands = null;
            _option.Dispose();
            _option = null;
        }


        //Tùy chỉnh
        ThongTinControl _infomation = null;
        void NewInfomationControl(object sender, EventArgs e)
        {
            Debug.Assert(_infomation == null);
            _Commands.ThongTinCommands.New.Enabled = false; // Disable new client command to prevent re-entrancy
            _infomation = new ThongTinControl();
            _infomation.Commands = _Commands;
            this.ShowModalPanel(_infomation, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;
        }

        private void CancelInfomation(object sender, EventArgs e)
        {
            // Simply close client entry form "dialog"
            Debug.Assert(_infomation != null);
            CloseInfomation();
        }
        private void CloseInfomation()
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;

            _Commands.ThongTinCommands.New.Enabled = true; // Enable new client command

            this.CloseModalPanel(_infomation, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            _infomation.Commands = null;
            _infomation.Dispose();
            _infomation = null;
        }
        #region Command Execution

        //private void ChangeMetroThemeExecuted(object sender, EventArgs e)
        //{
        //    ICommandSource source = (ICommandSource)sender;
        //    MetroColorGeneratorParameters theme = (MetroColorGeneratorParameters)source.CommandParameter;
        //    StyleManager.MetroColorGeneratorParameters = theme;
        //}

        private void ToggleStartControlExecuted(object sender, EventArgs e)
        {
            _StartControl.IsOpen = !_StartControl.IsOpen;
        }



        #endregion

        #region UI Code
        void StartControl_Click(object sender, EventArgs e)
        {
            _StartControl.IsOpen = false;
        }

        protected override void OnLoad(EventArgs e)
        {
            UpdateControlsSizeAndLocation();
            base.OnLoad(e);
        }

        private Rectangle GetStartControlBounds()
        {
            int captionHeight = metroShell1.MetroTabStrip.GetCaptionHeight() + 2;
            Thickness borderThickness = this.GetBorderThickness();
            return new Rectangle((int)borderThickness.Left + 70, captionHeight, this.Width - (int)borderThickness.Horizontal, this.Height - captionHeight - 1);
        }
        private void UpdateControlsSizeAndLocation()
        {
            if (_StartControl != null)
            {
                if (!_StartControl.IsOpen)
                    _StartControl.OpenBounds = GetStartControlBounds();
                else
                    _StartControl.Bounds = GetStartControlBounds();
                if (!IsModalPanelDisplayed)
                    _StartControl.BringToFront();
            }

        }
        protected override void OnResize(EventArgs e)
        {
            UpdateControlsSizeAndLocation();
            base.OnResize(e);
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
        #endregion



    }
}